<?php
namespace common\services;

use common\models\Exhibition;
use common\models\UserFollow;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;

class UserFollowServices extends UserFollow
{

    /**
     * 用户关注
     * @param $params
     * @return array
     */
    public function search($params){
        $model = UserFollow::find(false);
        $model->orderBy('id desc');

        if (!empty($params['user_id'])) {
            $model->andFilterWhere(['=', 'user_id', $params['user_id']]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pagesize' => $params['defaultPageSize'],
            ]
        ]);

        if (isset($params['type']) && $params['type'] != '') {
            $model->andFilterWhere(['=', 'type', $params['type']]);
        }

        if (isset($params['status']) && $params['status'] != '') {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }

        if (!empty($params['keywords'])){
            $model->andFilterWhere(['or', ['like', 'object_id', $params['keywords']], ['like', 'user_id', $params['keywords']]]);
        }


        return $dataProvider;
    }

    /**
     * 判断用户是否关注
     * @param $userId
     * @param $objectType
     * @param $objectId
     * @return int
     */
    public function isFollow($userId, $objectType, $objectId) {
        $model = UserFollow::find(false);
        $model->andFilterWhere(['=', 'status', UserFollow::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'user_id', $userId])
            ->andFilterWhere(['=', 'type', $objectType])
            ->andFilterWhere(['=', 'object_id', $objectId]);
        $res = $model->count(1);
        return intval($res) > 0 ? 1 : 0;
    }

    /**
     * 关注操作
     * @param $userId
     * @param $objectType
     * @param $objectId
     * @return bool|int
     */
    public function goFollow($userId, $objectType, $objectId){
        $model = UserFollow::findOne(['user_id' => $userId, 'type' => $objectType, 'object_id' => $objectId]);
        if (!empty($model)) {
            if ($model->status == UserFollow::STATUS_FLAG_NORMAL) {
                //取消关注
                $model->status = UserFollow::STATUS_FLAG_STOP;
                $result = $model->update();
                //展会
                if ($result !== false && $objectType == UserFollow::USER_FOLLOW_TYPE_EXHIBITION) {
                    Exhibition::subFieldNumber($objectId, 'follow_number');
                }else{
                    //
                }
            }else{
                //重新关注
                $model->status = UserFollow::STATUS_FLAG_NORMAL;
                $result = $model->update();
                //展会
                if ($result !== false && $objectType == UserFollow::USER_FOLLOW_TYPE_EXHIBITION) {
                    Exhibition::addFieldNumber($objectId, 'follow_number');
                }else{
                    //
                }
            }
        }else{
            $new = new UserFollow();
            $new->type = $objectType;
            $new->object_id = $objectId;
            $new->user_id = $userId;
            $new->status = UserFollow::STATUS_FLAG_NORMAL;
            $result = $new->save();
            //展会
            if ($result !== false && $objectType == UserFollow::USER_FOLLOW_TYPE_EXHIBITION) {
                Exhibition::addFieldNumber($objectId, 'follow_number');
            }else{
                //
            }
        }
        return $result;
    }

    /**
     * 获取我关注的数据
     * @param $userId
     * @param $objectType
     * @param int $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getMyFollow($userId, $objectType, $limit = 12){
        $model = UserFollow::find(false);
        $model->andFilterWhere(['=', 'status', UserFollow::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'user_id', $userId])
            ->andFilterWhere(['=', 'type', $objectType]);

        $result = $model->limit($limit)->orderBy("id desc")->select("id,type,user_id,object_id")->asArray()->all();

        return $result;
    }

}
